﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Groovy_IM.Assets;
using System.Text;

namespace Groovy_IM
{
    public partial class AddAccounts : PhoneApplicationPage
    {
        private int sProtocol;

        public AddAccounts()
        {
            InitializeComponent();

            App.systemTrayBar(App.SystemTrayIsVisible);

            SupportedOrientations = App.Orientation;
        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string sTag;

            if ((sender as TextBlock) != null)
                sTag = (sender as TextBlock).Tag.ToString();
            else
                sTag = (sender as Image).Tag.ToString();

            sProtocol = Convert.ToInt32(sTag);

            proto.SelectedIndex = -1;

            AIM.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/nonselected.png", UriKind.Relative));
            MSN.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/nonselected.png", UriKind.Relative));
            MySpace.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/nonselected.png", UriKind.Relative));
            YIM.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/nonselected.png", UriKind.Relative));
            
            switch (sProtocol)
            {
                case 0:
                    AIM.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/Checkmark.png", UriKind.Relative));
                    Server.Text = "login.oscar.aol.com";
                    Port.Text = "5190";
                    break;
                case 1:
                    MSN.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/Checkmark.png", UriKind.Relative));
                    Server.Text = "gmail.com";
                    Port.Text = "5222";
                    break;
                case 2:
                    YIM.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/Checkmark.png", UriKind.Relative));
                    Server.Text = "scs.msg.yahoo.com";
                    Port.Text = "5050";
                    break;
                case 3:
                    MySpace.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/Checkmark.png", UriKind.Relative));
                    Server.Text = "im.myspace.akadns.net";
                    Port.Text = "1863";
                    break;
                case 4:
                    Jabber.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("Images/Protocols/Checkmark.png", UriKind.Relative));
                    Server.Text = "";
                    Port.Text = "";
                    break;
            }
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                accWidth.Width = 455;
                protoWidth.Width = 455;
            }
            else
            {
                accWidth.Width = 775;
                protoWidth.Width = 775;
            }
        }

        private void pivSwitch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateButtonMnu(0, null, null, false, null);
            updateMenuMnu(0, null, false, null);
            updateButtonMnu(0, null, null, false, null);
            updateMenuMnu(0, null, false, null);
            updateButtonMnu(0, null, null, false, null);
            updateMenuMnu(0, null, false, null);
            updateButtonMnu(0, null, null, false, null);
            updateMenuMnu(0, null, false, null);

            ApplicationBar.IsVisible = false;

            ApplicationBar.IsMenuEnabled = false;

            switch (pivSwitch.SelectedIndex)
            {
                case 2:
                    ApplicationBar.IsVisible = true;
                    ApplicationBar.IsMenuEnabled = true;
                    updateButtonMnu(0, "/Images/App/AppBar/appbar.add.rest.png", "add", true, new EventHandler(add));
                    updateButtonMnu(1, "/Images/App/AppBar/appbar.cancel.rest.png", "cancel", true, new EventHandler(cancel));
                    updateMenuMnu(0, "help", true, new EventHandler(help));
                    break;
            }
        }

        private void add(object sender, EventArgs e)
        {
            if (userName.Text.Trim().Length < 1 || passWord.Password.Trim().Length < 1 || Server.Text.Trim().Length < 1 || Port.Text.Trim().Length < 1)
            {
                MessageBox.Show("We cannot succesfully add your account, you have not fully filled out all of the details.");
                return;
            }

            App.m_Actions.AddAccount(sProtocol, userName.Text, passWord.Password, Server.Text, Convert.ToInt32(Port.Text), Description.Text, (bool)aLogin.IsChecked, (bool)mLogin.IsChecked, (bool)iLogin.IsChecked, (bool)sLoginDetails.IsChecked);
        
            NavigationService.GoBack();
        }
        
        private void cancel(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void help(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/HelpPage.xaml", UriKind.Relative));
        }

        private void updateButtonMnu(int Index, string uriString, string Text, bool Visibile, EventHandler Handler)
        {
            ApplicationBarIconButton button = null;

            if (ApplicationBar.Buttons.Count > Index)
            {
                button = ApplicationBar.Buttons[Index] as ApplicationBarIconButton;
            }

            if (button != null)
            {
                {
                    ApplicationBar.Buttons.Remove(button);
                }
            }

            if (Visibile == true)
            {
                button = new ApplicationBarIconButton(new Uri(uriString, UriKind.Relative));
                button.Text = Text;
                button.Click += Handler;
                ApplicationBar.Buttons.Insert(Index, button);
            }
        }

        private void updateMenuMnu(int Index, string Text, bool Visibile, EventHandler Handler)
        {
            ApplicationBarMenuItem menu = null;

            if (ApplicationBar.MenuItems.Count > Index)
            {
                menu = ApplicationBar.MenuItems[Index] as ApplicationBarMenuItem;
            }

            if (menu != null)
            {
                {
                    ApplicationBar.MenuItems.Remove(menu);
                }
            }

            if (Visibile == true)
            {
                menu = new ApplicationBarMenuItem(Text);
                menu.Text = Text;
                menu.Click += Handler;
                ApplicationBar.MenuItems.Insert(Index, menu);
            }
        }

        private void Description_LostFocus(object sender, RoutedEventArgs e)
        {
            if (Description.Text.Trim().Length < 1)
                Description.Text = "Personal Account";
        }

        private void Description_GotFocus(object sender, RoutedEventArgs e)
        {
            if (Description.Text == "Personal Account")
                Description.Text = "";
        }

        private void accDet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (accDet.SelectedIndex > -1)
                accDet.SelectedIndex = -1;
        }
    }
}